package com.taokatao.cardmee.business.cardcode;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.taokatao.cardmee.R;
import com.taokatao.cardmee.db.model.Card;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class CardCodeTabStrip extends LinearLayout implements View.OnClickListener {
    @BindView(R.id.i_code) ImageView iCode;
    @BindView(R.id.i_front) ImageView iFront;
    @BindView(R.id.i_back) ImageView iBack;
    @BindView(R.id.i_other) ImageView iOther;

    // fix bug
    private int mCodeFlag, mFrontFlag, mBackFlag, mOtherFlag;

    private Callback callback;

    public CardCodeTabStrip(Context context) {
        this(context, null);
    }

    public CardCodeTabStrip(Context context, AttributeSet attrs) {
        super(context, attrs);
        ButterKnife.bind(this, LayoutInflater.from(context).inflate(R.layout.layout_card_code_tab_strip, this));
    }

    public void setCallback(Callback c) {
        callback = c;
    }

    public void setData(Card c) {
        int visibleCount = 0;
        if (c != null) {
            if (!TextUtils.isEmpty(c.getMCardNo())
                    || !TextUtils.isEmpty(c.getCardUser()) || !TextUtils.isEmpty(c.getCardTel())) {
                visibleCount++;
                mCodeFlag = visibleCount;
                iCode.setVisibility(View.VISIBLE);
            } else {
                iCode.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(c.getPositiveIcon())) {
                visibleCount++;
                mFrontFlag = visibleCount;
                iFront.setVisibility(View.VISIBLE);
            } else {
                iFront.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(c.getBackIcon())) {
                visibleCount++;
                mBackFlag = visibleCount;
                iBack.setVisibility(View.VISIBLE);
            } else {
                iBack.setVisibility(View.GONE);
            }

            visibleCount++;
            mOtherFlag = visibleCount;
            iOther.setVisibility(View.VISIBLE);

        }
        setVisibility(visibleCount > 1 ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    @OnClick({R.id.i_code, R.id.i_front, R.id.i_back, R.id.i_other})
    public void onClick(View v) {
        int clickedMenu = Menu.None;
        switch (v.getId()) {
            case R.id.i_code:
                clickedMenu = Menu.Code;
                break;
            case R.id.i_front:
                clickedMenu = Menu.IFront;
                break;
            case R.id.i_back:
                clickedMenu = Menu.IBack;
                break;
            case R.id.i_other:
                clickedMenu = Menu.OTHER;
                break;
        }
        clickTab(clickedMenu);
    }

    public void clickTab(int clickedMenu) {
        clickTab(clickedMenu, true);
    }

    public void clickTab(int clickedMenu, boolean hasCallback) {
        int mappingPositionOfViewPager = 0;
        switch (clickedMenu) {
            case Menu.Code:
                iCode.setImageResource(R.mipmap.card_code_code_selected);
                iFront.setImageResource(R.mipmap.card_code_front_normal);
                iBack.setImageResource(R.mipmap.card_code_back_normal);
                iOther.setImageResource(R.mipmap.card_code_other_normal);
                mappingPositionOfViewPager = mCodeFlag;
                break;
            case Menu.IFront:
                iCode.setImageResource(R.mipmap.card_code_code_normal);
                iFront.setImageResource(R.mipmap.card_code_front_selected);
                iBack.setImageResource(R.mipmap.card_code_back_normal);
                iOther.setImageResource(R.mipmap.card_code_other_normal);
                mappingPositionOfViewPager = mFrontFlag;
                break;
            case Menu.IBack:
                iCode.setImageResource(R.mipmap.card_code_code_normal);
                iFront.setImageResource(R.mipmap.card_code_front_normal);
                iBack.setImageResource(R.mipmap.card_code_back_selected);
                iOther.setImageResource(R.mipmap.card_code_other_normal);
                mappingPositionOfViewPager = mBackFlag;
                break;
            case Menu.OTHER:
                iCode.setImageResource(R.mipmap.card_code_code_normal);
                iFront.setImageResource(R.mipmap.card_code_front_normal);
                iBack.setImageResource(R.mipmap.card_code_back_normal);
                iOther.setImageResource(R.mipmap.card_code_other_selected);
                mappingPositionOfViewPager = mOtherFlag;
                break;
            default:
                clickedMenu = Menu.None;
                break;
        }

        if (hasCallback && clickedMenu != Menu.None && callback != null) {
            callback.onTabStripClick(mappingPositionOfViewPager);
        }
    }


    public interface Callback {
        void onTabStripClick(int selected);
    }

    public static class Menu {
        final static int None = 0;
        final static int Code = 1;
        final static int IFront = 2;
        final static int IBack = 3;
        final static int OTHER = 4;
    }

}
